//
//  UIBarButtonItem+Extensions.swift
//  WB
//
//  Created by 汪鹏 on 2016/12/18.
//  Copyright © 2016年 汪鹏. All rights reserved.
//

import Foundation

extension UIBarButtonItem{
    
    
    /// 实例化UIBarButtonItem（遍历构造方法一般用来快速自定义组件）
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize,默认16
    ///   - target: target
    ///   - action: action
    ///   - isBack: 判断是否是返回按钮
    convenience init(title:String,fontSize:CGFloat = 16,target:AnyObject,action:Selector, isBack:Bool = false) {
        
        // swift不能识别oc中返回值为instancetype的对象，所以为可选的，可以加上类型来解决
        let leftBtn:UIButton = UIButton.cz_textButton(title, fontSize: fontSize, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        
        if isBack {
            
            let imageName = "navigationbar_back_withtext";
            
            leftBtn.setImage(UIImage.init(named: imageName), for: .normal)
            
            leftBtn.setImage(UIImage.init(named: imageName + "_highlighted"), for: .highlighted)
            
            leftBtn.sizeToFit()
            
        }
        
        leftBtn.addTarget(target, action: action, for: .touchUpInside)
        
        self.init(customView: leftBtn)
    }
    
}
